package com.jinniulive.platform.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jinniulive.platform.R;
import com.jinniulive.platform.bean.NewsListInfo;

import java.util.List;

/**
 * Created by Administrater on 2016/12/9.
 */

public class NewsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<NewsListInfo.NewsdataBean.SamplenewsBean> mData;
    private Context mContext;
    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        void itemClickListener(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public NewsListAdapter(Context context) {
        mContext = context;
    }

    public void setData(List<NewsListInfo.NewsdataBean.SamplenewsBean> data) {
        if (data != null) {
            Log.i("TAG", "数据不为空");
            mData = data;
            notifyDataSetChanged();
        }
    }

    private class BodyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView textView;

        BodyViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.tv_news_content);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            int position = getPosition();
            mListener.itemClickListener(view, position);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new BodyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_news_body, parent, false));
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((BodyViewHolder) holder).textView.setText(mData.get(position).getTitle());

    }

    @Override
    public int getItemCount() {
        return mData != null ? mData.size() : 0;
    }
}
